import 'package:flutter/material.dart';
import '../model/productModel.dart';
import '../services/productService.dart';

class ProductProvider extends ChangeNotifier {
  final ProductService _productService = ProductService();

  List<ProductModel> _products = [];
  bool _isLoading = false;
  String? _error;

  List<ProductModel> get products => _products;
  bool get isLoading => _isLoading;
  String? get error => _error;

  Future<void> fetchProducts() async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      _products = await _productService.getProducts();
      print('成功获取到 ${_products.length} 个商品');
    } catch (e) {
      _error = e.toString();
      print('获取商品失败: $e');
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }
}
